package com.dx.mes.service.process.service;

import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPPACKCLM_STATE_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPUPKCLM_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FPPUPKCLM;
import static com.dx.mes.common.bean.def.GenericDef._NORMAL;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.process.service.IFppoutpackclmService;
import com.dx.mes.facade.process.tx.Fpoutpackclm.FpoutpackclmI;
import com.dx.mes.facade.process.tx.Fpoutpackclm.FpoutpackclmIA;
import com.dx.mes.facade.process.tx.Fpoutpackclm.FpoutpackclmO;
import com.dx.mes.facade.process.tx.Fpoutpackclm.FpoutpackclmOA;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dx.mes.common.bean.def.GenericDef.*;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;


/**
 * Created by admin on 2017/11/2.
 */
@Service("fppoutpackclmService")
public class FppoutpackclmService implements IFppoutpackclmService {


    private LogUtils logUtils;
    @Autowired
    private IRetPrdInfoDao retPrdInfoDao;

    @Autowired
    private IRetPrdPackDao retPrdPackDao;

    @Autowired
    private IRetBoxDao retBoxDao;
    @Autowired
    private IRetCrtNoDao retCrtNoDao;
    @Autowired
    private IBisMdlDefDao bisMdlDefDao;
    @Autowired
    private IBisMdlGrdDao bisMdlGrdDao;


    @Override
    public String subMainProc(String evt_no,String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(FppoutpackclmService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FppoutpackclmService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");

        String strOutMsg = null;
        FpoutpackclmI inTrx = new FpoutpackclmI();
        FpoutpackclmO outTrx = new FpoutpackclmO();

        Session session = retPrdInfoDao.getCrSession();

        outTrx.setTrx_id(T_FPPUPKCLM);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
        try {
            inTrx = JacksonUtil.fromJson(strInMsg, FpoutpackclmI.class);

            char action_flg = inTrx.getAction_flg().charAt(0);
            switch (action_flg) {
                case ACTION_FLG_QUERY:
                    queryByConditionFnc(inTrx,outTrx);
                    break;
                case  'F':
                    queryInBox(inTrx,outTrx);
                    break;
                case ACTION_FLG_ADD:
                    addFnc(inTrx,outTrx);
                    break;
                case ACTION_FLG_DELETE:
                    delFnc(inTrx,outTrx);
                    break;
                default:
                    outTrx = errActionFnc();
            }
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
                session.flush();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            outTrx.setRtn_mesg("程序执行异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            strOutMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + strOutMsg + "]");
        }
        return strOutMsg;
    }

    public void  queryByConditionFnc(FpoutpackclmI inTrx,FpoutpackclmO outTrx) {
        FpoutpackclmIA iary = inTrx.getIary().get(0);
        String boxIdFk = iary.getWbox_id();
         //String woIdFk  = iary.getWo_id_fk();
         List<Ret_box> ret_boxs = new ArrayList<>();
        List<FpoutpackclmOA> oary= new ArrayList<>();
        if(!"".equals(boxIdFk) && boxIdFk!=null){
            String hql ="From Ret_box where 1=1 and ship_box_id ='" + boxIdFk +"'";
           /* if(!"".equals(woIdFk)&&woIdFk!=null){
                hql = hql + " and wo_id_fk='" + woIdFk+"'";
            }*/
            ret_boxs = retBoxDao.find(hql);
            if(ret_boxs.size()>0){
                int prdQty=0;
                for (Ret_box ret_box : ret_boxs){
                    prdQty = prdQty+ret_box.getPrd_qty();
                }
                FpoutpackclmOA fpoutpackclmOA = new FpoutpackclmOA();
                fpoutpackclmOA.setBox_id_fk(ret_boxs.get(0).getShip_box_id());
                fpoutpackclmOA.setBox_id(ret_boxs.get(0).getBox_id());
                fpoutpackclmOA.setBox_id_fk1(ret_boxs.get(0).getBox_id());
                fpoutpackclmOA.setBox_id_fk2(ret_boxs.get(0).getBox_id());
                fpoutpackclmOA.setWo_id_fk(ret_boxs.get(0).getWo_id_fk());
                fpoutpackclmOA.setMdl_id_fk(ret_boxs.get(0).getMdl_id_fk());
                fpoutpackclmOA.setPrd_grade(ret_boxs.get(0).getPrd_grade_fk());
                fpoutpackclmOA.setPrd_qty(String.valueOf(prdQty));
                //根据箱子带的产品 带出box pack数量
                String mdlIdFk = ret_boxs.get(0).getMdl_id_fk();
                Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdlIdFk);
                Integer boxCnt = bis_mdl_def.getBox_cnt();
                fpoutpackclmOA.setBox_cnt(boxCnt);
                oary.add(fpoutpackclmOA);
            }
         }else {
            String hql = "select distinct ship_box_id From Ret_box";
            List<String> boxIdFkList = retBoxDao.find(hql);
            for(String boxId : boxIdFkList){
                int prdQty=0;
                String hqls ="From Ret_box where 1=1 and ship_box_id ='"+boxId+"'";
                /*if(!"".equals(woIdFk)&&woIdFk!=null){
                    hqls = hqls + " and wo_id_fk='" + woIdFk+"'";
                }*/
                ret_boxs = retBoxDao.find(hqls);
                if(ret_boxs.size()>0){
                    for (Ret_box ret_box : ret_boxs){
                        prdQty = prdQty+ret_box.getPrd_qty();
                    }
                    FpoutpackclmOA fpoutpackclmOA = new FpoutpackclmOA();
                    fpoutpackclmOA.setBox_id_fk(ret_boxs.get(0).getShip_box_id());//外
                    fpoutpackclmOA.setBox_id(ret_boxs.get(0).getBox_id());//内
                    fpoutpackclmOA.setBox_id_fk1(ret_boxs.get(0).getBox_id());
                    fpoutpackclmOA.setBox_id_fk2(ret_boxs.get(0).getBox_id());
                    fpoutpackclmOA.setWo_id_fk(ret_boxs.get(0).getWo_id_fk());
                    fpoutpackclmOA.setMdl_id_fk(ret_boxs.get(0).getMdl_id_fk());
                    fpoutpackclmOA.setPrd_grade(ret_boxs.get(0).getPrd_grade_fk());
                    fpoutpackclmOA.setPrd_qty(String.valueOf(prdQty));
                    //根据箱子带的产品 带出box pack数量
                    String mdlIdFk = ret_boxs.get(0).getMdl_id_fk();
                    Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdlIdFk);
                    Integer boxCnt = bis_mdl_def.getBox_cnt();
                    fpoutpackclmOA.setBox_cnt(boxCnt);
                    oary.add(fpoutpackclmOA);
                }
            }
         }
        outTrx.setOary(oary);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);

    }

    public void queryInBox(FpoutpackclmI inTrx,FpoutpackclmO outTrx){
        FpoutpackclmIA iary = inTrx.getIary().get(0);
        String boxIdFk = iary.getWbox_id();
        String hql ="From Ret_box where 1=1 and ship_box_id ='" + boxIdFk +"'";
        List<Ret_box> ret_boxs = retBoxDao.find(hql);
        List<FpoutpackclmOA> oary= new ArrayList<>();
        for(int i = 0;i <ret_boxs.size();i++){
            FpoutpackclmOA fpoutpackclmOA = new FpoutpackclmOA();
            fpoutpackclmOA.setBox_id_fk(ret_boxs.get(i).getShip_box_id());//外
            fpoutpackclmOA.setBox_id(ret_boxs.get(i).getBox_id());//内
            fpoutpackclmOA.setBox_id_fk1(ret_boxs.get(i).getBox_id());
            fpoutpackclmOA.setBox_id_fk2(ret_boxs.get(i).getBox_id());
            fpoutpackclmOA.setWo_id_fk(ret_boxs.get(i).getWo_id_fk());
            fpoutpackclmOA.setMdl_id_fk(ret_boxs.get(i).getMdl_id_fk());
            fpoutpackclmOA.setPrd_grade(ret_boxs.get(i).getPrd_grade_fk());
            fpoutpackclmOA.setPrd_qty(ret_boxs.get(i).getPrd_qty().toString());
            //根据箱子带的产品 带出box pack数量
            String mdlIdFk = ret_boxs.get(i).getMdl_id_fk();
            Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdlIdFk);
            Integer boxCnt = bis_mdl_def.getBox_cnt();
            fpoutpackclmOA.setBox_cnt(boxCnt);
            oary.add(fpoutpackclmOA);
        }
        outTrx.setOary(oary);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);

    }

    public SimpleRtnBeen queryfirstBox(String boxId){
        String gsf = "";
        Ret_box ret_box = retBoxDao.get(boxId);
        if(ret_box == null){
            gsf = "箱号[" + ret_box + "]不存在";
            return new SimpleRtnBeen(Long.valueOf(RETURN_CODE_UNKNOWN) ,gsf);
        }
        if(!"COMP".equals(ret_box.getBox_stat())){
            gsf = "箱号[" + boxId + "]的状态不是完成状态";
            return new SimpleRtnBeen(Long.valueOf(RETURN_CODE_UNKNOWN) ,gsf);
        }
        if(ret_box.getShip_box_id()!=null){
            gsf = "箱号[" + boxId + "]已经被合并";
            return new SimpleRtnBeen(Long.valueOf(RETURN_CODE_UNKNOWN) ,gsf);
        }
        List<Ret_prd_info> ret_prd_info1 = getPrdInfo(boxId);
        for(Ret_prd_info retPrdInfo1 : ret_prd_info1){
            if("INPR".equals(retPrdInfo1.getPrd_stat())){
                gsf = "箱号[" + boxId + "]与中的玻璃[" + retPrdInfo1.getPrd_seq_id() + "]还在制程中";
                return new SimpleRtnBeen(Long.valueOf(RETURN_CODE_UNKNOWN) ,gsf);
            }
        }
        return new SimpleRtnBeen(RETURN_CODE_OK);
    }

    public void  addFnc(FpoutpackclmI inTrx,FpoutpackclmO outTrx) {
        FpoutpackclmIA iary = inTrx.getIary().get(0);
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        String evtUsr = inTrx.getEvt_usr();
        List<String> box_ids = iary.getBox_ids();
        SimpleRtnBeen rtnBeen= queryfirstBox(box_ids.get(0));
        if (rtnBeen.getlRc() != _NORMAL) {
            String gszMsgBuf = rtnBeen.getMsgBuf();
            outTrx.setRtn_mesg(gszMsgBuf);
            return;
        }
        Ret_box ret_boxf =  retBoxDao.get(box_ids.get(0));
        List<Ret_box> ret_boxes = new ArrayList<>();
        ret_boxes.add(ret_boxf);
        for(int i = 1 ;i < box_ids.size(); i++){
          Ret_box ret_box = retBoxDao.get(box_ids.get(i));
            if(ret_box == null){
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("箱号[" + ret_box + "]不存在");
                return;
            }
            if(!"COMP".equals(ret_box.getBox_stat())){
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("箱号[" + box_ids.get(0) + "]的状态不是完成状态");
                return;
            }
            if(ret_box.getShip_box_id()!=null){
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("箱号[" + box_ids.get(0) + "]已经被合并");
                return;
            }

            if(!ret_box.getWo_id_fk().equals(ret_boxf.getWo_id_fk())){
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("箱号[" + ret_box.getBox_id() + "]与箱号[" + ret_boxf.getBox_id() + "]的不是同一个工单");
                return;
            }
            if(!ret_box.getPrd_grade_fk().equals(ret_boxf.getPrd_grade_fk())){
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("箱号[" + ret_box.getBox_id() + "]与箱号[" + ret_boxf.getBox_id() + "]的不是同一个等级");
                return;
            }

            List<Ret_prd_info> ret_prd_info1 = getPrdInfo(box_ids.get(i));
            for(Ret_prd_info retPrdInfo1 : ret_prd_info1){
                if("INPR".equals(retPrdInfo1.getPrd_stat())){
                    outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                    outTrx.setRtn_mesg("箱号[" + box_ids.get(0) + "]与中的玻璃[" + retPrdInfo1.getPrd_seq_id() + "]还在制程中");
                    return;
                }
            }
            ret_boxes.add(ret_box);
        }
        String mdlIdFk = ret_boxf.getMdl_id_fk();
        Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdlIdFk);
        Integer boxCnt = bis_mdl_def.getMax_pf_cnt();
        if(boxCnt == null){
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("产品["+bis_mdl_def.getMdl_id()+"]未设置PACK数量！请重新确认!");
            return;
        }
        if(boxCnt < box_ids.size()){
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("产品["+bis_mdl_def.getMdl_id()+"]的PACK数量为["+boxCnt+"]，合并的箱子数量为["+box_ids.size()+"]，请重新确认!");
            return;
        }

        Integer prdQtys =0 ;
        String boxIdFk = getBoxId(ret_boxf.getMdl_id_fk(),ret_boxf.getPrd_grade_fk(),evtUsr,outTrx);//生成新箱子
        for (int i = 0;i < ret_boxes.size();i++){
            ret_boxes.get(i).setShip_box_id(boxIdFk);
            ret_boxes.get(i).setEvt_usr(evtUsr);
            ret_boxes.get(i).setEvt_timestamp(current_timestamp);
            //计算所有的prdQty
            prdQtys += ret_boxes.get(i).getPrd_qty();
        }
        retBoxDao.saveOrUpdateAll(ret_boxes);

        //更新外箱子
        //TODO 生成标签
        Ret_box ret_boxw = new Ret_box();
        ret_boxw.setBox_id(boxIdFk);
        if(ret_boxf.getMdl_id_fk() != null){
            ret_boxw.setMdl_id_fk(ret_boxf.getMdl_id_fk());
        }
        if(ret_boxf.getWo_id_fk() != null){
           ret_boxw.setWo_id_fk(ret_boxf.getWo_id_fk());
         }
         if(ret_boxf.getMdl_id_fk() != null){
             ret_boxw.setMdl_id_fk(ret_boxf.getMdl_id_fk());
        }
        if(ret_boxf.getPrd_grade_fk() !=null){
            ret_boxw.setPrd_grade_fk(ret_boxf.getPrd_grade_fk());
        }
        ret_boxw.setPrd_qty(prdQtys);
        ret_boxw.setBox_stat("COMP");
        ret_boxw.setReal_emp("N");//真空标记为N
        ret_boxw.setBox_cate("W");//外箱
        if(ret_boxf.getBatch_no_fk()!= null){
            ret_boxw.setBatch_no_fk(ret_boxf.getBatch_no_fk());
        }
        ret_boxw.setEvt_usr(evtUsr);
        ret_boxw.setEvt_timestamp(current_timestamp);
        retBoxDao.save(ret_boxw);

        //ret_prd_info
        List<Ret_prd_info> ret_prd_infoAll = new ArrayList<>();
        for(int i = 0; i < box_ids.size(); i++){
            List<Ret_prd_info> ret_prd_infos= getPrdInfo(box_ids.get(i));
            if(ret_prd_infos.size() > 0){
                for (int j = 0; j < ret_prd_infos.size(); j++){
                    Ret_prd_info ret_prd_info = ret_prd_infos.get(j);
                    ret_prd_info.setShip_box_id_fk(boxIdFk);
                    ret_prd_info.setEvt_timestamp(current_timestamp);
                    ret_prd_info.setEvt_cate(EVT_OUTER_BOX_PACK);
                    ret_prd_info.setEvt_usr(evtUsr);
                    ret_prd_infoAll.add(ret_prd_info);
                }
            }
        }
        retPrdInfoDao.saveOrUpdateAll(ret_prd_infoAll);

        List<FpoutpackclmOA> oary= new ArrayList<>();
        FpoutpackclmOA fpoutpackclmOA = new FpoutpackclmOA();
        fpoutpackclmOA.setBox_id_fk(boxIdFk);
        fpoutpackclmOA.setWo_id_fk(ret_boxw.getWo_id_fk());
        fpoutpackclmOA.setMdl_id_fk(ret_boxw.getMdl_id_fk());
        fpoutpackclmOA.setPrd_grade(ret_boxw.getPrd_grade_fk());
        fpoutpackclmOA.setPrd_qty(String.valueOf(prdQtys));
        fpoutpackclmOA.setMdl_desc(bis_mdl_def.getMdl_dsc());
        oary.add(fpoutpackclmOA);
        outTrx.setOary(oary);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }

    public void  delFnc(FpoutpackclmI inTrx,FpoutpackclmO outTrx) {
        FpoutpackclmIA iary = inTrx.getIary().get(0);
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        String eveUsr = inTrx.getEvt_usr();
        String boxIdFk = iary.getWbox_id();

        String hql ="From Ret_box where 1=1 ";
        if(!"".equals(boxIdFk) && boxIdFk!=null){
            hql = hql + " and ship_box_id ='" + boxIdFk +"'";
        }
        List<Ret_box> ret_boxs = retBoxDao.find(hql);
        if(ret_boxs.size()<0){
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("内箱箱号[" + boxIdFk + "]的信息不存在");
            return;
        }
        for(Ret_box ret_box :ret_boxs){
            if(!"COMP".equals(ret_box.getBox_stat())){
                outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
                outTrx.setRtn_mesg("箱号[" + ret_box.getBox_id() + "]的箱子不是COMP状态");
                return;
            }
            ret_box.setShip_box_id("");
            ret_box.setEvt_usr(eveUsr);
            ret_box.setEvt_timestamp(current_timestamp);
            retBoxDao.update(ret_box);
        }

        //更新玻璃
        //ret_prd_info
        for(int i = 0; i < ret_boxs.size(); i++){
            String boxId = ret_boxs.get(i).getBox_id();
            List<Ret_prd_info> ret_prd_infos= getPrdInfo(boxId);
            if(ret_prd_infos.size() > 0){
                for (int j = 0; j < ret_prd_infos.size(); j++){
                    ret_prd_infos.get(j).setShip_box_id_fk(_SPACE);
                    ret_prd_infos.get(j).setEvt_timestamp(current_timestamp);
                    ret_prd_infos.get(j).setEvt_cate(EVT_OUTER_BOX_PACK_CANCEL);
                    ret_prd_infos.get(j).setEvt_usr(eveUsr);
                }
                retPrdInfoDao.saveOrUpdateAll(ret_prd_infos);
            }
        }

        //删除外箱信息
        Ret_box ret_box =  retBoxDao.get(boxIdFk);
        if(ret_box != null){
            retBoxDao.delete(ret_box);
        }

        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }
    public FpoutpackclmO errActionFnc() {
        FpoutpackclmO fpoutpackclmO = new FpoutpackclmO();
        fpoutpackclmO.setTrx_id(T_FPPUPKCLM);
        fpoutpackclmO.setType_id(TRX_OUT);
        fpoutpackclmO.setRtn_code(E_FPPUPKCLM_INVALID_INPUT + "");
        fpoutpackclmO.setRtn_mesg("Invalid Action_flg");
        return fpoutpackclmO;
    }
    public  List<Ret_prd_info> getPrdInfo(String boxIdFk){

        String hql="From Ret_prd_info where box_id_fk ='"+boxIdFk+"'";
        List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.find(hql);
        return ret_prd_infos;
    }

    public String   getBoxId(String mdlIdFk,String prdGrade,String evtUsr,FpoutpackclmO outTrx) {

        String timeCdoe= Util.getEvtTimeforVer();
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        String code="";
        String hql = "From Bis_mdl_grd where mdl_id_fk='*' and prd_grade='" + prdGrade+ "'";
        List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hql);
        if(bis_mdl_grds.size()>0){
            code=bis_mdl_grds.get(0).getGrade_prt();
        }else {
            outTrx.setRtn_code(E_FPPPACKCLM_STATE_ERROR + "");
            outTrx.setRtn_mesg("产品等级信息没有维护");
        }
        String boxIdFk = "W"+timeCdoe;
        Ret_crt_noId ret_crt_noId = new Ret_crt_noId();
        ret_crt_noId.setKey_id(boxIdFk);
        ret_crt_noId.setType("W");
        Ret_crt_no ret_crt_no = retCrtNoDao.get(ret_crt_noId);
        Ret_crt_no ret_crt_noNew = new Ret_crt_no();
        if(ret_crt_no==null){
            boxIdFk = boxIdFk+code+"Z"+"001";
            ret_crt_noNew.setId(ret_crt_noId);
            ret_crt_noNew.setSerial_no("001");
            ret_crt_noNew.setEvt_usr(evtUsr);
            ret_crt_noNew.setEvt_timestamp(current_timestamp);
            retCrtNoDao.save(ret_crt_noNew);
        }else {
            String strA = ret_crt_no.getSerial_no();
            String strB =String.format("%0"+3+"d", Integer.parseInt(strA)+1);
            boxIdFk = boxIdFk +code+ "Z"+strB;
            ret_crt_no.setSerial_no(strB);
            ret_crt_no.setEvt_usr(evtUsr);
            ret_crt_no.setEvt_timestamp(current_timestamp);
            retCrtNoDao.update(ret_crt_no);
        }
        return boxIdFk;
    }
}
